// formPackage/pages/platformModule/agencyInvitePage/agencyInvitePage.js
import { queryInviteAgentQrCode } from "../../../../service/platform_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    menuHeight: 0,
    customActionStyle: {},
    menuTop: 0,
    posterImg: "https://saas.img.ylyc.top/bg/inviteagentposbgbb_icon.png",//海报图片
    //赋值的
    posterInfo: {},
    qrCodeImg: "",//二维码
    //改变值的:
    posterData: {
      "width": "650rpx",
      "height": "1240rpx",
      "background": "transparent",
      "views": [

      ]
    },
    posterViews: [
      //背景图
      {
        "type": "image",
        "url": "https://saas.img.ylyc.top/bg/inviteagentposbgbb_icon.png",
        "css": {
          "width": "650rpx",
          "height": "1240rpx",
          "top": "0rpx",
          "left": "0rpx",
          "rotate": "0",
          "borderRadius": "",
          "borderWidth": "",
          "shadow": "",
          "mode": ""
        }
      },
      //头像
      {
        "type": "image",
        "url": "",
        "css": {
          "width": "80rpx",
          "height": "80rpx",
          "top": "460rpx",
          "right": "70rpx",
          "rotate": "0",
          "borderRadius": "40rpx",
          "borderWidth": "",
          "borderColor": "#000000",
          "shadow": "",
          "mode": "aspectFill"
        }
      },
      //昵称
      {
        "type": "text",
        "text": "",
        "css": {
          top: "460rpx",
          right: "190rpx",
          textAlign: 'right',
          padding: '10rpx',
          scalable: true,
          deletable: true,
          "fontSize": "34rpx",
          "fontWeight": "bold",
          "color": "#000000",
        }
      },
      // 提示
      {
        "type": "text",
        "text": "",
        "css": {
          top: "510rpx",
          right: "190rpx",
          textAlign: 'right',
          padding: '10rpx',
          scalable: true,
          deletable: true,
          "fontSize": "24rpx",
          "fontWeight": "normal",
          "color": "#666666",
        }
      },

      //个人二维码
      {
        "type": "image",
        "url": "",
        "css": {
          "width": "130rpx",
          "height": "130rpx",
          "top": "430rpx",
          "left": "70rpx",
          "rotate": "0",
          "borderRadius": "",
          "borderWidth": "",
          "borderColor": "#000000",
          "shadow": "",
          "mode": "aspectFill"
        }
      },
    ],
    isCreatePoster: false,//是否已经生成海报
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      menuTop: app.globalData.menuButton.top,
      menuHeight: app.globalData.menuButton.height
    })
    this.createProsterAction();
    this.queryInviteAgentQrCode();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  createProsterAction() {
    wx.showLoading({
      title: '海报生成中',
    })

    this.configHousePosterData();
  },
  //获取分享二维码
  queryInviteAgentQrCode() {
    queryInviteAgentQrCode().then(res => {
      console.log("JJLog:二维码是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          qrCodeImg: res.data || ""
        })
        if (this.data.qrCodeImg) {
          let qrCodeItem = 'posterViews[' + 4 + '].url';
          this.setData({
            [qrCodeItem]: this.data.qrCodeImg,
            'posterData.views': this.data.posterViews,
            posterInfo: this.data.posterData,
          })
        }
      }
    }).catch(err => {
      console.error("JJLog:queryInviteAgentQrCode", err);
    })
  },

  //配置海报
  configHousePosterData() {
    //动态数组:
    let avatarItem = 'posterViews[' + 1 + '].url';
    let nikcItem = 'posterViews[' + 2 + '].text';
    let remindItem = 'posterViews[' + 3 + '].text';
    // let qrCodeItem = 'posterViews[' + 4 + '].url';
    let nickName = "";
    if (app.globalData.name) {
      nickName = app.globalData.name;
    } else {
      nickName = app.globalData.nickName
    }
    if (nickName.length > 5) {
      nickName = nickName.substr(0, 5) + "...";
    }

    this.setData({
      [avatarItem]: app.globalData.avatar || "",
      [nikcItem]: nickName,
      // [qrCodeItem]: "https://saas.img.ylyc.top/bg/cposteribg_icon.png",
      [remindItem]: "邀请您入驻小程序"
    })
    // this.setData({
    //   'posterData.views': this.data.posterViews,
    //   posterInfo: this.data.posterData,
    // })


  },

  //创建海报成功
  createPosterSuccess(e) {
    wx.hideLoading();
    wx.showToast({
      title: '生成成功',
      icon: "success"
    })
    console.log("JJLog:path:" + e.detail.path);
    this.setData({
      posterImg: e.detail.path,
      isCreatePoster: true,
    });
  },

  //创建海报失败
  createPosterFail() {
    wx.hideLoading();
    wx.showToast({
      title: "海报生成失败",
      icon: "none"
    })
    console.error("JJLog:生成海报失败:" + JSON.stringify(e));
  },


  //保存海报名片
  savePosterImg() {
    let that = this;
    if (!this.data.isCreatePoster) {
      wx.showToast({
        title: '海报生成失败，无法保存',
        icon: "none"
      })
      return;
    }
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: () => {
              // 同意授权
              //保存到本地
              wx.saveImageToPhotosAlbum({
                filePath: this.data.posterImg,
                success(res) {
                  wx.showToast({
                    title: '保存海报成功',
                    icon: 'success',
                    duration: 2000
                  })
                  that.setData({
                    showPosterImgView: false
                  })
                },
                fail: (err) => {
                  console.log("JJLog:保存图片失败:" + JSON.stringify(err));
                  wx.showToast({
                    title: '保存海报失败',
                    icon: "none"
                  })
                }
              })
            },
            fail: (res) => {
              console.log(res);
              wx.showModal({
                title: '提示',
                content: '保存海报需要您同意访问您的相册',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting();
                  } else if (res.cancel) {
                    // console.log('用户点击取消')
                  }
                }
              })


            }
          })
        } else {
          // 已经授权了
          //保存到本地
          wx.saveImageToPhotosAlbum({
            filePath: this.data.posterImg,
            success(res) {
              wx.showToast({
                title: '保存海报成功',
                icon: 'success',
                duration: 2000
              })
              that.setData({
                showPosterImgView: false
              })
            },
            fail: (err) => {
              console.log("JJLog:保存图片失败:" + JSON.stringify(err));
              wx.showToast({
                title: '保存海报失败',
                icon: "none"
              })
            }
          })
        }
      },
      fail: (res) => {
        console.log(res);
      }
    })
  },



})